import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_day_stat.dart';
import '../../../../net/dio_utils.dart';
import '../../../../utils/date_utils.dart';
import '../../../../utils/toast_utils.dart';

class MyScoreRecoderController extends GetxController {
  String myScoreCurrentSelectDate =
      ViDateUtils.formatDate(DateTime.now(), format: "yyyy-MM");

  ///当前选中的时间 日期选择器使用
  DateTime selectedDate = DateTime.now();

  Future<void> selectDate(BuildContext context) async {
    final DateTime date = await DatePicker.showDatePicker(context,
        // 是否展示顶部操作按钮
        showTitleActions: true,
        // 最小时间
        minTime: DateTime(2020, 1, 1),
        // 最大时间
        maxTime: DateTime(2099, 6, 7),
        // change事件
        onChanged: (date) {
      print('change $date');
    },
        // 确定事件
        onConfirm: (date) {
      print('confirm $date');
    },
        // 当前时间
        currentTime: DateTime.now(),
        // 语言
        locale: LocaleType.zh);

    if (date == null) {
      ToastUtils.showToast("日期格式不正常 请重新选择");
      return;
    }
    selectedDate = date;
    String dateStr = ViDateUtils.formatDate(date, format: "yyyy-MM");
    myScoreCurrentSelectDate = dateStr;
    update(["selectTimes"]);
    //获取数据
    initLoad(click: true);
  }

  int _pageIndex = 1;
  int _pageSize = 30;
  int _totalPage = 0;

  bool _loading = true;
  List<BeanDayStat> _list = [];

  List<BeanDayStat> getDayStatList() {
    return _list;
  }

  bool getIsLoading() {
    return _loading;
  }

  ///查询我的答题记录
  Future<List<BeanDayStat>> getTopicHistoryist() async {
    Map<String, dynamic> monthMap = {
      "sort": "TIMES",
      "pageName": "MineMyScoreRecoderPage",
      "pageIndex": _pageIndex,
      "startDate": "$myScoreCurrentSelectDate-01 00:00:00",
      "limit": _pageSize
    };

    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.myMyScoreQuestionRecoderUrl, jsonMap: monthMap);
    _list = [];
    _loading = false;
    if (statisRresponseInfo.success) {
      List list = statisRresponseInfo.data['list'];
      _totalPage = statisRresponseInfo.data['totalPage'];
      List<BeanDayStat> themList = [];
      list.forEach((element) {
        BeanDayStat stat = BeanDayStat.fromMap(element);
        if (stat != null) {
          themList.add(stat);
        }
      });
      if (_pageIndex == 1) {
        _list = themList;
      } else {
        _list.addAll(themList);
      }
      update(["root"]);
      return _list;
    } else {
      ToastUtils.showToast(statisRresponseInfo.message);
      update(["root"]);
      return _list;
    }
  }

  Future<List<BeanDayStat>> initLoad({bool click = false}) async {
    _list = [];
    _loading = true;
    update(["root"]);
    await getTopicHistoryist();
    return _list;
  }

  Future<List<BeanDayStat>> loadMore() async {
    if ((_pageIndex) < _totalPage && !_loading) {
      _pageIndex++;
      await getTopicHistoryist();
    }
    return _list;
  }
}
